package com.buba.springcloud.controller;

import com.buba.springcloud.entities.CommonResult;
import com.buba.springcloud.entities.HfStaff;
import com.buba.springcloud.entities.LoginUser;
import com.buba.springcloud.service.BLoginService;
import com.buba.springcloud.util.JwtUtil;
import com.buba.springcloud.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/backstagesupporter/main")
public class BLoginController {
    @Resource
    private BLoginService bLoginService;

    @Resource
    private RedisUtil redisUtil;
    @PostMapping("/login")
    public CommonResult login(@RequestBody LoginUser user) {
        HfStaff staffInfo = bLoginService.getStaffInfo(user.getUsername());
        if (staffInfo!=null){
            if (staffInfo.getPassword().equals(user.getPassword())) {
                String jwt = JwtUtil.createJWT(1000 * 30 * 30, staffInfo);
                redisUtil.set("buser"+String.valueOf(staffInfo.getId()), staffInfo,60);
                return new CommonResult(200, "登录成功",jwt);
            }
        }
        return new CommonResult(500, "登录失败");
    }
}
